/*
 * Created on Oct 26, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.erclab.internal.xpresso;

import com.erclab.internal.xpresso.conf.*;
import com.erclab.internal.xpresso.dataAccess.FormDAO;
import com.erclab.internal.xpresso.exceptions.FormException;
import com.erclab.internal.xpresso.forms.*;
import com.erclab.internal.xpresso.util.ExceptionUtil;
import com.erclab.internal.xpresso.webapp.JavascriptFormValidator;


/**
 * @author rodrigen
 *
 */
public class FormFactory extends ObjectFactory {

	public static Form loadForm(String formName) throws FormException{
		
		// TODO: Log with log4j request of form and time
   		if (formName == null){
   			throw new FormException("A form name has to be provided in order to load the form");
   		}
		// TODO: Caching mechanism   	
		FormDAO dao = Settings.getRepository().getDaoFactory().getFormDAO();
    	Form theForm = dao.createForm(formName);
		if(theForm == null)
			throw new FormException("The form '" + formName + "' could not be found in the repository.");
		
		// Load the helper class
		String formHelperClass = theForm.getFormHelperClass();
		if (formHelperClass != null && !formHelperClass.equals(""))
			loadFormHelper(formHelperClass, theForm);
		
		// Load form elements
		Settings.getRepository().getDaoFactory().getFormDAO().loadFormElements(theForm);
 	
		// TODO: Change this call to call the interface instead of the concrete class
		// Settings.getClientFormValidator() ....
		JavascriptFormValidator jfv = new JavascriptFormValidator();
		jfv.setClientScript(theForm);

		//	TODO: Log with log4j end of of form constructor and time    
		return theForm;
	}
	
	protected static void loadFormHelper(String formHelperClass, Form theForm) throws FormException {
		FormHelper fh = null;
		try {
			Class formHelper = Class.forName(formHelperClass);
			fh = (FormHelper) formHelper.newInstance();
			theForm.setFormHelper(fh);
		} catch(ClassNotFoundException e){
			throw new FormException("The class: " + formHelperClass + " could not be found." + e);
		} catch(IllegalAccessException e){
			throw new FormException("IllegalAccessException: " + e);
		} catch(InstantiationException e){
			throw new FormException("InstantiationException" + e);				
		}
	}

	public static FormResultSet createForm(Form theForm) throws SecurityException, Exception {
		
		// Check permission to run this report
		if (!Settings.getAccessController().isAllowedToUseResource(theForm, theForm.getFormParameters().getUser())){
			throw new SecurityException(ExceptionUtil.getSecurityExceptionString(theForm));			
		}
		
		// Call the before creation method of the form helper
		if (theForm.getFormHelper() != null)
			theForm.getFormHelper().beforeFormCreation(theForm);		
		
		FormResultSet frs = new FormResultSet(theForm);

		// Call the after creation method of the form helper
		if (theForm.getFormHelper() != null)
			theForm.getFormHelper().afterFormCreation(theForm);
		return frs;
	}


}
